package com.xsky.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import sun.misc.BASE64Decoder;

public class Compresser
{
	/**
	 * 将字符串的byte数组压缩，并以字符串数组的方式输出
	 * @param bytes
	 * @return
	 */
	public static final byte[]compress(byte[] bytes)
	{
		byte[] compressed;
		ByteArrayOutputStream out = null;
		ZipOutputStream zout = null;

		try
		{
			out = new ByteArrayOutputStream();
			zout = new ZipOutputStream(out);
			zout.putNextEntry(new ZipEntry("0"));
			zout.write(bytes);
			zout.closeEntry();
			compressed = out.toByteArray();
		} catch (IOException e)
		{
			compressed = null;
		} finally
		{
			if (zout != null)
			{
				try
				{
					zout.close();
				} catch (IOException e)
				{
				}
			}
			if (out != null)
			{
				try
				{
					out.close();
				} catch (IOException e)
				{
				}
			}
		}
		return compressed;
	}
	/**
	 * 将字符串压缩成字符串数组并输出
	 * @param str
	 * @return
	 */
	public static final byte[] compress(String str)
	{
		if (str == null)
		{
			return null;
		}
		byte[] compressed;
		ByteArrayOutputStream out = null;
		ZipOutputStream zout = null;

		try
		{
			out = new ByteArrayOutputStream();
			zout = new ZipOutputStream(out);
			zout.putNextEntry(new ZipEntry("0"));
			zout.write(str.getBytes());
			zout.closeEntry();
			compressed = out.toByteArray();
		} catch (IOException e)
		{
			compressed = null;
		} finally
		{
			if (zout != null)
			{
				try
				{
					zout.close();
				} catch (IOException e)
				{
				}
			}
			if (out != null)
			{
				try
				{
					out.close();
				} catch (IOException e)
				{
				}
			}
		}
		return compressed;
	}
	/**
	 * 将字符串数组压缩成base64的字符串并输出
	 * @param bytes
	 * @return
	 */
	public static final String compressToBase64Str(byte[] bytes)
	{
		byte[] compressed;
		ByteArrayOutputStream out = null;
		ZipOutputStream zout = null;

		try
		{
			out = new ByteArrayOutputStream();
			zout = new ZipOutputStream(out);
			zout.putNextEntry(new ZipEntry("0"));
			zout.write(bytes);
			zout.closeEntry();
			compressed = out.toByteArray();
		} catch (IOException e)
		{
			compressed = null;
		} finally
		{
			if (zout != null)
			{
				try
				{
					zout.close();
				} catch (IOException e)
				{
				}
			}
			if (out != null)
			{
				try
				{
					out.close();
				} catch (IOException e)
				{
				}
			}
		}
		return new sun.misc.BASE64Encoder().encodeBuffer(compressed);//base64的字符串
	}
	/**
	 * 将字符串压缩后再经过base64编码并输出 先压缩 后编码 即 普通字符串->压缩->编码
	 * @param str
	 * @return
	 */
	public static final String compressToBase64Str(String str)
	{
		byte[] compressed;
		ByteArrayOutputStream out = null;
		ZipOutputStream zout = null;

		try
		{
			out = new ByteArrayOutputStream();
			zout = new ZipOutputStream(out);
			zout.putNextEntry(new ZipEntry("0"));
			zout.write(str.getBytes());
			zout.closeEntry();
			compressed = out.toByteArray();
		} catch (IOException e)
		{
			compressed = null;
		} finally
		{
			if (zout != null)
			{
				try
				{
					zout.close();
				} catch (IOException e)
				{
				}
			}
			if (out != null)
			{
				try
				{
					out.close();
				} catch (IOException e)
				{
				}
			}
		}
		return new sun.misc.BASE64Encoder().encodeBuffer(compressed);//base64的字符串
	}
	/**
	 * 将压缩的字符串数组解压成字符串并输出
	 * @param compressed
	 * @return
	 */
	public static final String decompress(byte[] compressed)
	{
		if (compressed == null)
			return null;

		ByteArrayOutputStream out = null;
		ByteArrayInputStream in = null;
		ZipInputStream zin = null;
		String decompressed;
		try
		{
			out = new ByteArrayOutputStream();
			in = new ByteArrayInputStream(compressed);
			zin = new ZipInputStream(in);
			ZipEntry entry = zin.getNextEntry();
			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = zin.read(buffer)) != -1)
			{
				out.write(buffer, 0, offset);
			}
			decompressed = out.toString();
		} catch (IOException e)
		{
			decompressed = null;
		} finally
		{
			if (zin != null)
			{
				try
				{
					zin.close();
				} catch (IOException e)
				{
				}
			}
			if (in != null)
			{
				try
				{
					in.close();
				} catch (IOException e)
				{
				}
			}
			if (out != null)
			{
				try
				{
					out.close();
				} catch (IOException e)
				{
				}
			}
		}

		return decompressed;
	}
	
	/**
	 * 将压缩的base64字符串解压并还原成普通字符串并输出 先还原 后解压 即 base64编码的压缩字符串->解码->解压缩->普通字符串
	 * @param base64Str  
	 * @return
	 */
	public static final String decompressFromBase64Str(String base64Str)
	{
		byte[] compressed = null;
		try
		{
			compressed = new BASE64Decoder().decodeBuffer(base64Str);
		} catch (IOException e1)
		{
			e1.printStackTrace();
		}
		if (compressed == null)
			return null;

		ByteArrayOutputStream out = null;
		ByteArrayInputStream in = null;
		ZipInputStream zin = null;
		String decompressed;
		try
		{
			out = new ByteArrayOutputStream();
			in = new ByteArrayInputStream(compressed);
			zin = new ZipInputStream(in);
			ZipEntry entry = zin.getNextEntry();
			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = zin.read(buffer)) != -1)
			{
				out.write(buffer, 0, offset);
			}
			decompressed = out.toString();
		} catch (IOException e)
		{
			decompressed = null;
		} finally
		{
			if (zin != null)
			{
				try
				{
					zin.close();
				} catch (IOException e)
				{
				}
			}
			if (in != null)
			{
				try
				{
					in.close();
				} catch (IOException e)
				{
				}
			}
			if (out != null)
			{
				try
				{
					out.close();
				} catch (IOException e)
				{
				}
			}
		}

		return decompressed;
	}
}
